#include <cmath>
#include "PWM.h"

double PWM::generate_triangle_wave()
{
    double k = amptitude * 4 * frequency;
    inner_time = fmod(*simulation_time, cycle);
    if (inner_time < cycle * 0.25)
    {
        return k * inner_time;
    }
    else if (inner_time < 0.75 * cycle)
    {
        return -k * inner_time + 2 * amptitude;
    }
    else
    {
        return k * inner_time - 4 * amptitude;
    }
}

int PWM::compare(double modulation_wave)
{
    carrier = generate_triangle_wave();

    if (modulation_wave > carrier)
    {
        if (modulation_wave > 0)
            return 1;
        else
            return 0;
    }
    else
    {
        if (modulation_wave > 0)
            return 0;
        else
            return -1;
    }
}